/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import tableModels.TableEtapeModel;
import utils.Connexion;

/**
 *
 * @author Betty
 */
public class EtapeMapper {
    private Connexion _connexion;
    
    public EtapeMapper() {
        try {
            this._connexion = new Connexion();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TableEtapeModel fetchAll() {
        String requete = "SELECT ID, IDENTIFIANTCIRCUIT, ORDRE, DATEETAPE, DUREE, NOMLIEU, VILLE FROM PROJET.ETAPE";
        String[] titre = {"IDENTIFIANT","IDENTIFIANT CIRCUIT","ORDRE","DATE ETAPE","DUREE", "NOM", "VILLE"};
        TableEtapeModel model = null;
        try {
            model = new TableEtapeModel(this._connexion,requete,titre);
            model.getRowCount();
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
            return model;
    }
    
    private void update(Etape etape) {
        try {
            String requete = "UPDATE PROJET.ETAPE SET IDENTIFIANTCIRCUIT='" + etape.getIdentifiantCircuit() + "',"
                    + "ORDRE='" + etape.getOrdre() +"', DATEETAPE='" + etape.getDateEtape() +"',"
                    + "DUREE='" + etape.getDuree() + "',NOM='" + etape.getNomLieu() + "',"
                    + "VILLE='" + etape.getVille() +  "'"
                    + " WHERE ID=" + etape.getIdentifiant();
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(EtapeMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Etape find(int id, Etape etape) throws SQLException  {
        String requete = "SELECT ID, IDENTIFIANTCIRCUIT, ORDRE, DATEETAPE, DUREE, NOMLIEU, VILLE FROM PROJET.ETAPE WHERE ID=" + id;
        this._connexion.setQuery(requete);
        ResultSet resultat = this._connexion.getResultSet();
        while(resultat.next()){
            etape.setIdentifiant(Integer.parseInt(resultat.getString("IDENTIFIANT")));
            etape.setIdentifiantCircuit(Integer.parseInt(resultat.getString("IDENTIFIANTCIRCUIT")));
            etape.setOrdre(Integer.parseInt(resultat.getString("ORDRE")));
            etape.setDateEtape(resultat.getString("DATEETAPE"));
            etape.setNomLieu(resultat.getString("NOMLIEU"));
            etape.setDuree(Integer.parseInt(resultat.getString("DUREE")));
            etape.setVille(resultat.getString("VILLE"));
            
        }
        return etape;
    }
    
    public void save(Etape etape) {
        if(etape.getIdentifiant()== 0){
            this.add(etape);
        } else {
           this.update(etape);
        }
    }
    
    private void add(Etape etape) {
        try {
            String requete = "INSERT INTO PROJET.ETAPE VALUES(0,'" + etape.getIdentifiantCircuit() + "',"
                    + "'" + etape.getOrdre() + "','" + etape.getDateEtape() +"',"
                    + "'" + etape.getDuree() + "','" + etape.getNomLieu() + "',"
                    + "'" + etape.getVille() + "')";
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(EtapeMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
